//
//
// *  $Id: TestRecordManager.java,v 1.6 2003/09/21 15:49:02 boisvert Exp $
//
// *
//
// *  Unit tests for RecordManager class
//
// *
//
// *  Simple db toolkit
//
// *  Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
//
// *
//
// *  This library is free software; you can redistribute it and/or
//
// *  modify it under the terms of the GNU Library General Public License
//
// *  as published by the Free Software Foundation; either version 2
//
// *  of the License, or (at your option) any later version.
//
// *
//
// *  This library is distributed in the hope that it will be useful,
//
// *  but WITHOUT ANY WARRANTY; without even the implied warranty of
//
// *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//
// *  Library General Public License for more details.
//
// *
//
// *  You should have received a copy of the GNU Library General Public License
//
// *  along with this library; if not, write to the Free Software Foundation,
//
// *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
//
// 


//*************************************************************************
//**  Included in JDMB 1.0 port to SharpDBM;  11/2013 Cyrus Neah cneah@codingreal.com
//**  SharpDBM is an independent reimplementation of the JDBM 1.0 software library  
//*************************************************************************



using SharpDBM;
using SharpDBM.recman;
using SharpDBM.helper;
using SharpDBM.btree;
using NUnit.Framework;



namespace SharpDBM.Test

.recman
{



	using RecordManager = SharpDBM.RecordManager;

	using RecordManagerFactory = SharpDBM.RecordManagerFactory;



	using NUnit.Framework;


///
//
// *  This class contains all Unit tests for {@link RecordManager}.
//
// 

	[TestFixture]
    public class TestRecordManager  
	{



		



		[Test]
 public virtual void setUp()
		{

			TestRecordFile.deleteTestFile();

		}



		[Test]
 public virtual void tearDown()
		{

			TestRecordFile.deleteTestFile();

		}



//    *
//
//     *  Test constructor
//
//     

 
//
		[Test]
 public virtual void testCtor()

		{

			RecordManager recman;



			recman = RecordManagerFactory.createRecordManager(TestRecordFile.testFileName);

			recman.close();

		}



//    *
//
//     *  Test basics
//
//     

 
//
		[Test]
 public virtual void testBasics()

		{

			RecordManager recman;



			recman = new BaseRecordManager(TestRecordFile.testFileName);



		// insert a 10,000 byte record.

			sbyte[] data = TestUtil.makeRecord(10000, (sbyte) 1);

			long rowid = recman.insert(data);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid), 10000, (sbyte)1), "check data1");



		// update it as a 20,000 byte record.

			data = TestUtil.makeRecord(20000, (sbyte) 2);

			recman.update(rowid, data);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid), 20000, (sbyte)2), "check data2");



		// insert a third record.

			data = TestUtil.makeRecord(20, (sbyte) 3);

			long rowid2 = recman.insert(data);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid2), 20, (sbyte)3), "check data3");



		// now, grow the first record again

			data = TestUtil.makeRecord(30000, (sbyte) 4);

			recman.update(rowid, data);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid), 30000, (sbyte)4), "check data4");





		// delete the record

			recman.delete(rowid);



		// close the file

			recman.close();

		}



//    *
//
//     *  Test delete and immediate reuse. This attempts to reproduce
//
//     *  a bug in the stress test involving 0 record lengths.
//
//     

 
//
		[Test]
 public virtual void testDeleteAndReuse()

		{

			RecordManager recman;



			recman = new BaseRecordManager(TestRecordFile.testFileName);



		// insert a 1500 byte record.

			sbyte[] data = TestUtil.makeRecord(1500, (sbyte) 1);

			long rowid = recman.insert(data);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid), 1500, (sbyte)1), "check data1");





		// delete the record

			recman.delete(rowid);



		// insert a 0 byte record. Should have the same rowid.

			data = TestUtil.makeRecord(0, (sbyte) 2);

			long rowid2 = recman.insert(data);

            NUnit.Framework.Assert.AreEqual(rowid, rowid2, "old and new rowid");

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid2), 0, (sbyte)2), "check data2");



		// now make the record a bit bigger

			data = TestUtil.makeRecord(10000, (sbyte) 3);

			recman.update(rowid, data);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid), 10000, (sbyte)3), "check data3");



		// .. and again

			data = TestUtil.makeRecord(30000, (sbyte) 4);

			recman.update(rowid, data);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid), 30000, (sbyte)4), "check data3");



		// close the file

			recman.close();

		}



//    *
//
//     *  Test rollback sanity. Attemts to add a new record, rollback and
//
//     *  add the same record.  We should obtain the same record id for both
//
//     *  operations.
//
//     

 
//
		[Test]
 public virtual void testRollback()

		{

			RecordManager recman;



		// Note: We start out with an empty file

			recman = new BaseRecordManager(TestRecordFile.testFileName);



		// insert a 150000 byte record.

			sbyte[] data1 = TestUtil.makeRecord(150000, (sbyte) 1);

			long rowid1 = recman.insert(data1);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid1), 150000, (sbyte)1), "check data1");



		// rollback transaction, should revert to previous state

			recman.rollback();



		// insert same 150000 byte record.

			sbyte[] data2 = TestUtil.makeRecord(150000, (sbyte) 1);

			long rowid2 = recman.insert(data2);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid2), 150000, (sbyte)1), "check data2");



            NUnit.Framework.Assert.AreEqual(rowid1, rowid2, "old and new rowid");



			recman.commit();



		// insert a 150000 byte record.

			data1 = TestUtil.makeRecord(150000, (sbyte) 2);

			rowid1 = recman.insert(data1);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid1), 150000, (sbyte)2), "check data1");



		// rollback transaction, should revert to previous state

			recman.rollback();



		// insert same 150000 byte record.

			data2 = TestUtil.makeRecord(150000, (sbyte) 2);

			rowid2 = recman.insert(data2);

            NUnit.Framework.Assert.IsTrue(TestUtil.checkRecord((sbyte[])recman.fetch(rowid2), 150000, (sbyte)2), "check data2");



            NUnit.Framework.Assert.AreEqual(rowid1, rowid2, "old and new rowid");



		// close the file

			recman.close();

		}



//    *
//
//     *  Runs all tests in this class
//
//     

		static void Main(string[] args)
		{

			// junit.textui.TestRunner.run(new TestSuite(typeof(TestRecordManager)));

		}

	}


}